package edu.fasta.sisinfo2.gui;

import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import edu.fasta.sisinfo2.net.InterfazComandoServer;
import edu.fasta.sisinfo2.poker.PokerControllerServer;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.Color;
import java.net.UnknownHostException;

public class ConfigurarServidor extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanelFondo("/Img/Login5.png", 0, 0);
	private JTextField puertoField;
	private JButton okButton = new JButton();
	private JButton cancelButton = new JButton();

	private PokerControllerServer PCS;
	private InterfazComandoServer comando;
	private Icon i1 = new ImageIcon(getClass().getResource("/Img/iniciar1.png"));
	private Icon i2 = new ImageIcon(getClass().getResource("/Img/iniciar2.png"));
	private Icon i3 = new ImageIcon(getClass().getResource("/Img/iniciar3.png"));
	private Icon s1 = new ImageIcon(getClass().getResource("/Img/Salir1.png"));
	private Icon s2 = new ImageIcon(getClass().getResource("/Img/Salir2.png"));
	private Icon s3 = new ImageIcon(getClass().getResource("/Img/Salir3.png"));
	private JTextField rondasField;
	private JTextField minField;
	private JTextField maxField;

	/**
	 * Create the dialog.
	 * 
	 * @throws UnknownHostException
	 */
	public ConfigurarServidor(PokerControllerServer unPCS) throws UnknownHostException {
		this.setPCS(unPCS);
		setTitle("Texas UFasta - Crear Servidor");
		setBounds(100, 100, 525, 412);
		this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		this.setResizable(false);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);	
		this.setLocationRelativeTo(null);
		
		{
			JLabel lblNombreDeUsuario = new JLabel("Puerto (1024 - 49151)");
			lblNombreDeUsuario.setBounds(55, 100, 164, 20);
			contentPanel.add(lblNombreDeUsuario);
		}
		{
			puertoField = new JTextField();
			puertoField.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e)
				   {
				      char caracter = e.getKeyChar();

				      // Verificar si la tecla pulsada no es un digito
				      if(((caracter < '0') ||
				         (caracter > '9')) &&
				         (caracter != '\b' /*corresponde a BACK_SPACE*/))
				         
				      {
				         e.consume();  // ignorar el evento de teclado
				      }
				   }
				@Override
				public void keyReleased(KeyEvent arg0) {
					int parseInt = -1;
					
					try {
						parseInt = Integer.parseInt(puertoField.getText());
					} catch (Throwable e) {
						
						arg0.consume();
					}
					
					if ((parseInt > 49151) || (parseInt < 1024)) {
						okButton.setEnabled(false);
					}else{
						okButton.setEnabled(true);
					}
				}
			});
			puertoField.setText("35000");
			puertoField.setToolTipText("");
			puertoField.setBounds(268, 101, 207, 20);
			contentPanel.add(puertoField);
			puertoField.setColumns(10);
		}
		{
			JLabel lblPassword = new JLabel("Jugadores minimos para iniciar (2)");
			lblPassword.setBounds(55, 152, 218, 20);
			contentPanel.add(lblPassword);
		}
		{
			JLabel lblIngresarPasswordNuevamente = new JLabel("Jugadores maximos (2 - 23)");
			lblIngresarPasswordNuevamente.setBounds(55, 200, 203, 20);
			contentPanel.add(lblIngresarPasswordNuevamente);
		}
		
		{
			JLabel lblCreacionDeNuevo = new JLabel("Configuracion del servidor.");
			lblCreacionDeNuevo.setForeground(Color.WHITE);
			lblCreacionDeNuevo.setFont(new Font("Tahoma", Font.BOLD, 11));
			lblCreacionDeNuevo.setBounds(33, 11, 186, 22);
			contentPanel.add(lblCreacionDeNuevo);
		}
		{
			okButton = new JButton( "", i1 );
			okButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					getPCS().setNumJugadoresMinimos(Integer.parseInt(minField.getText()));
					getPCS().setNumRondas(Integer.parseInt(rondasField.getText()));
					getPCS().setNumJugadoresMaximo(Integer.parseInt(maxField.getText()));
					getPCS().setPuerto(Integer.parseInt(puertoField.getText()));
					dispose();
					getPCS().resume();
				}
			});
			okButton.setRolloverIcon( i2 );
			okButton.setPressedIcon(i3);
			okButton.setBorder(BorderFactory.createEmptyBorder());
			okButton.setContentAreaFilled(false);
			okButton.setBounds(133, 301, 114, 39);
			contentPanel.add(okButton);
			
			
			okButton.setActionCommand("OK");
			getRootPane().setDefaultButton(okButton);
		}
		{
			cancelButton = new JButton( "", s1 );
			cancelButton.setRolloverIcon( s2 );
			cancelButton.setPressedIcon(s3);
			cancelButton.setBorder(BorderFactory.createEmptyBorder());
			cancelButton.setContentAreaFilled(false);
			cancelButton.setBounds(268, 301, 114, 39);
			contentPanel.add(cancelButton);
			cancelButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					dispose();
					System.exit(0);
				}
			});
			cancelButton.setActionCommand("Cancel");
		}
		
		JLabel lblCantidadDeRondas = new JLabel("Cantidad de rondas (1- 32000)");
		lblCantidadDeRondas.setBounds(55, 254, 203, 14);
		contentPanel.add(lblCantidadDeRondas);
		
		rondasField = new JTextField();
		rondasField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e)
			   {
			      char caracter = e.getKeyChar();

			      // Verificar si la tecla pulsada no es un digito
			      if(((caracter < '0') ||
			         (caracter > '9')) &&
			         (caracter != '\b' /*corresponde a BACK_SPACE*/))
			         
			      {
			         e.consume();  // ignorar el evento de teclado
			      }
			   }
			@Override
			public void keyReleased(KeyEvent arg0) {
				int parseInt = -1;
				
				try {
					parseInt = Integer.parseInt(rondasField.getText());
				} catch (Throwable e) {
					
					arg0.consume();
				}
				
				if ((parseInt < 1) || (parseInt > 32000)){
					okButton.setEnabled(false);
				}else{
					okButton.setEnabled(true);
				}
			}
		});
		rondasField.setText("2");
		rondasField.setBounds(268, 251, 207, 20);
		contentPanel.add(rondasField);
		rondasField.setColumns(10);
		
		minField = new JTextField();
		minField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e)
			   {
			      char caracter = e.getKeyChar();

			      // Verificar si la tecla pulsada no es un digito
			      if(((caracter < '0') ||
			         (caracter > '9')) &&
			         (caracter != '\b' /*corresponde a BACK_SPACE*/))
			         
			      {
			         e.consume();  // ignorar el evento de teclado
			      }
			   }
			@Override
			public void keyReleased(KeyEvent arg0) {
				int parseInt = -1;
				int parseIntMax = -1;
				
				try {
					parseInt = Integer.parseInt(minField.getText());
					parseIntMax = Integer.parseInt(maxField.getText());
				} catch (Throwable e) {
					
					arg0.consume();
				}
				
				if ((parseInt <= 1) ||(parseInt > 23) || (parseInt > parseIntMax)){
					okButton.setEnabled(false);
				}else{
					okButton.setEnabled(true);
				}
			}
		});
		minField.setText("2");
		minField.setBounds(268, 152, 207, 20);
		contentPanel.add(minField);
		minField.setColumns(10);
		
		maxField = new JTextField();
		maxField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e)
			   {
			      char caracter = e.getKeyChar();

			      // Verificar si la tecla pulsada no es un digito
			      if(((caracter < '0') ||
			         (caracter > '9')) &&
			         (caracter != '\b' /*corresponde a BACK_SPACE*/))
			         
			      {
			         e.consume();  // ignorar el evento de teclado
			      }
			   }
			@Override
			public void keyReleased(KeyEvent arg0) {
				int parseInt = -1;
				int parseIntMin = -1;
				
				try {
					parseInt = Integer.parseInt(maxField.getText());
					parseIntMin = Integer.parseInt(minField.getText());
				} catch (Throwable e) {
					
					arg0.consume();
				}
				
				if ((parseInt <= 1) || (parseInt >23) || (parseInt < parseIntMin)){
					okButton.setEnabled(false);
				}else{
					okButton.setEnabled(true);
				}
			}
		});
		maxField.setText("10");
		maxField.setBounds(268, 201, 207, 20);
		contentPanel.add(maxField);
		maxField.setColumns(10);
		{
			JLabel ipLabel = new JLabel(getPCS().obtenerIP());
			ipLabel.setFont(new Font("Tahoma", Font.BOLD, 15));
			ipLabel.setForeground(Color.BLACK);
			ipLabel.setBounds(268, 53, 173, 14);
			contentPanel.add(ipLabel);
		}
		{
			JLabel lblDireccionIp = new JLabel("Direccion IP");
			lblDireccionIp.setBounds(55, 53, 112, 14);
			contentPanel.add(lblDireccionIp);
		}
	}

	public JTextField getTextField() {
		return puertoField;
	}

	public void setTextField(JTextField textField) {
		this.puertoField = textField;
	}

	public InterfazComandoServer getComando() {
		return comando;
	}

	public void setComando(InterfazComandoServer comando) {
		this.comando = comando;
	}

	public PokerControllerServer getPCS() {
		return PCS;
	}

	public void setPCS(PokerControllerServer pCS) {
		PCS = pCS;
	}
}
